package com.neusoft.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.entity.Student;
import com.neusoft.entity.common.CommonResult;
import com.neusoft.listener.StudentExcelLisener;
import com.neusoft.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;


/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2020-05-30 18:52:32
 */
@Controller
@RequestMapping("student")
public class StudentController {
    /**
     * 服务对象
     */

    @Autowired
    private StudentService studentService;

    @RequestMapping("toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("toWelcome")
    public String toWelcome() {
        return "welcome";
    }

    @RequestMapping("toList")
    public String toList(){
        return "user/student_list";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("bean",new Student());
        return "user/student_add";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer id,Model model){
        System.out.println("id:::"+ id);
        Student bean = studentService.queryById(id);
        model.addAttribute("bean",bean);
        return "user/student_add";
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     *
     */
    @GetMapping("getById")
    @ResponseBody
    public Student getById(Integer id) {
        return this.studentService.queryById(id);
    }
/*


    @PostMapping("login")
    public String login(String account, String password, Model model) {
        Student student = studentService.login(account, password);
        if (student == null) {
            //登录不成功
            model.addAttribute("msg", "账号或密码错误");
            return "login";
        }
        return "index";

        //使用Cookie记住账号和密码

        //将用户信息存入到session

    }
*/

    //分页查询
    @GetMapping("queryAll")
    @ResponseBody
    public Object queryAll(Integer page, Integer limit, Student bean){
        System.out.println("page:::" + page);
        System.out.println("limit:::" + limit);
        System.out.println("bean:::" + bean);
        CommonResult<Student> result = new CommonResult<>();
        IPage<Student> iPage = studentService.queryAllByLimit(page,limit,bean);
        result.setCode(0);
        result.setCount(iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }

    @PostMapping("save")
    @ResponseBody
    public Object save(Student bean){
        boolean result = false;
        //判断账号是否存在

        //判断是添加还是编辑
        if(bean.getId() != null) {
            //编辑
            result = studentService.update(bean)>0;
        }else{
            //添加

            result = studentService.insert(bean).getId() !=null;
        }
        return result;
    }

    @PostMapping("insert")
    @ResponseBody
    public Student insert(Student bean) {
        return studentService.insert(bean);
    }

    @PostMapping("deleteById")
    @ResponseBody
    public boolean deleteById(Integer[] ids){
        if(ids == null || ids.length == 0){
            return false;
        }
        studentService.deleteById(Arrays.asList(ids));
        return true;
    }
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("upload")
    @ResponseBody
    public Object upload(MultipartFile file) {
        CommonResult<String> result = new CommonResult<>();
        try {
            EasyExcel.read(file.getInputStream(), Student.class, new StudentExcelLisener(studentService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            result.setMsg("Excel上传出错");
        }
        result.setData(file.getOriginalFilename());
        return result;
    }
}